package com.baiytfp.hf.HFDataExchange.action.dao.impl;

import com.baiytfp.hf.baiytfp.common.db.DBManager;
import com.baiytfp.hf.baiytfp.framework.model.base.TableName;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import com.baiytfp.hf.HFDataExchange.action.dao.TreeDAOImpl;
import com.baiytfp.hf.HFDataExchange.action.dao.UserDAOImpl;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class OracleTreeDAOImpl extends TreeDAOImpl {

    // 更新子结点的父结点
    public long updateChildrenSeqCode(String seqCode, String oldSeqCode, int deepDiff, String treeType) throws Exception {

        StringBuffer sql = new StringBuffer();
        sql.append("update " + TableName.TREE + " set SEQ_CODE=? || substr(SEQ_CODE, ?, length(SEQ_CODE) - ?), ");
        sql.append(" PARENT_SEQ_CODE=? || substr(PARENT_SEQ_CODE, ?, length(PARENT_SEQ_CODE) - ?),  ");
        sql.append(" NODE_DEEP=NODE_DEEP+? where SEQ_CODE like ? and TREE_TYPE=?   ");

        List params = new ArrayList();
        params.add(seqCode);
        params.add(oldSeqCode.length() + 1);
        params.add(oldSeqCode.length());
        params.add(seqCode);
        params.add(oldSeqCode.length() + 1);
        params.add(oldSeqCode.length());
        params.add(deepDiff);
        params.add(oldSeqCode + "%");
        params.add(treeType);


        return HFDBHelper.executeSql(sql.toString(), params.toArray());

    }
}
